package pers.wensheng.patterns.creator_pattern.singleton_pattern.destroy_and_solve.reflex_mode;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


/**
 *  测试反射破环单例模式，以及防御机制
 */
public class Main {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1.获取字节码对象
        Constructor<Singleton> constructor = Singleton.class.getDeclaredConstructor();
        //取消访问检查，暴力反射
        constructor.setAccessible(true);

        //破环了单例模式
        Singleton singleton = constructor.newInstance();
        Singleton singleton2 = constructor.newInstance();
        System.out.println(singleton);
        System.out.println(singleton2);
    }
}
